﻿using Gaea.CodeFirst.Model;
using System;
using System.Linq;
using System.Reflection;

namespace Gaea.CodeFirst
{
    public class GaeaCodeFirstBasicAdapter
    {
        private readonly ITable _table;
        public GaeaCodeFirstBasicAdapter(ITable table)
        {
            _table = table;
        }

        public void CreateTable(OperateTableContext operateTableContext)
        {
            //operateTableContext.gaeaBasicPower = (GaeaBasicPower)Activator.CreateInstance(operateTableContext.typeOfGaeaBasicPower);
            _table.CreateTable(operateTableContext);
        }

        public void AlterTable(OperateTableContext operateTableContext)
        {
            //operateTableContext.gaeaBasicPower = (GaeaBasicPower)Activator.CreateInstance(operateTableContext.typeOfGaeaBasicPower);
            _table.AlterTable(operateTableContext);
        }

        /// <summary>
        /// 根据找到的继承了GaeaBasicPower的Model进行相关数据库操作
        /// </summary>
        /// <param name="assembly"></param>
        public void CompareTable(Assembly[] assemblys)
        {
            assemblys = assemblys.Where(x => !x.FullName.Contains("Microsoft") && !x.FullName.Contains("System"))
   .ToArray();
            OperateTableContext operateTableContext = new OperateTableContext();
            foreach (var assembly in assemblys)
            {
                var types = assembly.GetTypes().Where(x => typeof(GaeaBasicPower).IsAssignableFrom(x));
                var getTableNameTypes = assembly.GetTypes().Where(x => typeof(GaeaSon).IsAssignableFrom(x));
                var gaeaSon = "";
                foreach (var type in types)
                {
                    var tableName = "";
                    operateTableContext.typeOfGaeaBasicPower = type;
                    if (type.BaseType.IsGenericType)
                    {
                        gaeaSon = type.BaseType.FullName; //继承子类的具体字段名
                    }
                    else
                    {
                        return;
                    }

                    if (gaeaSon.IndexOf("MySql") != -1)//上面得到gaeaSon内关键词有mysql的
                    {
                        foreach (var getTableNameType in getTableNameTypes)
                        {
                            tableName = getTableNameType.CustomAttributes.ToList()[0].ConstructorArguments[0].Value.ToString();
                            operateTableContext.TableName = tableName;
                            operateTableContext.typeOfGaeaSon = getTableNameType;
                            operateTableContext.DbName = EnumDbName.MySql;
                            //根据继承GaeaMySqlPower的类名去数据库内搜索是否存在，不存在的话CreateNewTable，存在的话AlterTable
                            ExecuteTable(operateTableContext);
                        }
                    }
                    else if (gaeaSon.IndexOf("SqlServer") != -1)
                    {
                        foreach (var getTableNameType in getTableNameTypes)
                        {
                            tableName = getTableNameType.CustomAttributes.ToList()[0].ConstructorArguments[0].Value.ToString();
                            operateTableContext.TableName = tableName;
                            operateTableContext.typeOfGaeaSon = getTableNameType;
                            operateTableContext.DbName = EnumDbName.SqlServer;
                            //根据继承GaeaMySqlPower的类名去数据库内搜索是否存在，不存在的话CreateNewTable，存在的话AlterTable
                            ExecuteTable(operateTableContext);
                        }
                    }
                    else if (gaeaSon.IndexOf("Oracle") != -1)
                    {
                        foreach (var getTableNameType in getTableNameTypes)
                        {
                            tableName = getTableNameType.CustomAttributes.ToList()[0].ConstructorArguments[0].Value.ToString();
                            operateTableContext.TableName = tableName;
                            operateTableContext.typeOfGaeaSon = getTableNameType;
                            operateTableContext.DbName = EnumDbName.Oracle;
                            //根据继承GaeaMySqlPower的类名去数据库内搜索是否存在，不存在的话CreateNewTable，存在的话AlterTable
                            ExecuteTable(operateTableContext);
                        }
                    }
                }
            }

        }

        public void ExecuteTable(OperateTableContext operateTableContext)
        {
            var gaeaBasicPower = (GaeaBasicPower)Activator.CreateInstance(operateTableContext.typeOfGaeaBasicPower);
            operateTableContext.gaeaBasicPower = gaeaBasicPower;
            //查询数据库里所有表
            if (operateTableContext.DbName == EnumDbName.MySql)
            {
                var allTables = gaeaBasicPower.Find<string>("show tables ", new { });
                var enumerator = allTables.GetEnumerator();

                int i = 0;
                while (enumerator.MoveNext())
                {
                    string DBTableName = enumerator.Current;
                    if (DBTableName == operateTableContext.TableName) //名字相同，表存在，修改表
                    {
                        AlterTable(operateTableContext);
                        i = 0;
                        break;
                    }
                    i++;
                }
                if (i != 0) //即数据库里没有那个表，新建表
                {
                    CreateTable(operateTableContext);
                }

            }
            else if (operateTableContext.DbName == EnumDbName.SqlServer)
            {

                var allTables = gaeaBasicPower.Find<SysTables>("select * from sys.tables", new { });
                var enumerator = allTables.GetEnumerator();


                int i = 0;
                while (enumerator.MoveNext())
                {
                    string DBTableName = enumerator.Current.name;
                    if (DBTableName == operateTableContext.TableName) //名字相同，表存在，修改表
                    {
                        AlterTable(operateTableContext);
                        i = 0;
                        break;
                    }
                    i++;
                }
                if (i != 0) //即数据库里没有那个表，新建表
                {
                    CreateTable(operateTableContext);
                }
            }
            else if (operateTableContext.DbName == EnumDbName.Oracle)
            {
                var allTables = gaeaBasicPower.Find<Tabs>("select * from tabs", new { });
                var enumerator = allTables.GetEnumerator();

                int i = 0;
                while (enumerator.MoveNext())
                {
                    string DBTableName = enumerator.Current.TABLE_NAME;
                    if (DBTableName == operateTableContext.TableName) //名字相同，表存在，修改表
                    {
                        AlterTable(operateTableContext);
                        i = 0;
                        break;
                    }
                    i++;
                }
                if (i != 0) //即数据库里没有那个表，新建表
                {
                    CreateTable(operateTableContext);
                }
            }

        }


    }
}
